﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;


    private NewBehaviourScript player;
    private Door doorexit;

    public bool gameOver;
    public List<Enemy> enemies = new List<Enemy>();
    public void Awake()
    {
        if (instance == null)
            instance = this;
        else
            Destroy(gameObject);
         //player = FindObjectOfType<NewBehaviourScript>();
       // doorexit = FindObjectOfType<Door>();
    }

    public void Update()
    {
        if(player != null)
        gameOver = player.isDead;

        UIManager.instance.GameoverUI(gameOver);
    }

    public void IsEnemy(Enemy enemy)
    {
        enemies.Add(enemy);
    }

    public void EnemyDead(Enemy enemy)
    {
        enemies.Remove(enemy);
        if (enemies.Count == 0)
        {
            doorexit.OpenDoor();
            SaveData();
        }
    }
    public void IsPlayer(NewBehaviourScript controller)
    {
        player = controller;
    }

    public void Isdoor(Door door)
    {
        doorexit = door;
    }
    public void RestarSence()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        PlayerPrefs.DeleteKey("playerHealth");
        UIManager.instance.ResumeGame();
    }
    public void NewGame()
    {
        PlayerPrefs.DeleteAll();
        SceneManager.LoadScene(1);
    }
    public void Continute()
    {
        if (PlayerPrefs.HasKey("sceneIndex"))
        {
            SceneManager.LoadScene(PlayerPrefs.GetInt("sceneIndex"));
            Debug.Log("运行保存的场景");
        }
            
        else
            NewGame();
    }
    public void Gotomain()
    {
        SceneManager.LoadScene(0);
        Time.timeScale = 1;
    }
    public void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
    public void Quitgame()
    {
        Application.Quit();
    }
    public float LoadHealth()
    {
        if(!PlayerPrefs.HasKey("playerHealth"))
        {
            PlayerPrefs.SetFloat("playerHealth", 3f);
        }

        float currentHealth = PlayerPrefs.GetFloat("playerHealth");
        

        return currentHealth;
    }

    public void SaveData()
    {
        
        PlayerPrefs.SetFloat("playerHealth",player.Health);
        PlayerPrefs.SetInt("sceneIndex",SceneManager .GetActiveScene ().buildIndex + 1);
        Debug.Log("已保存数据");
        PlayerPrefs.Save();

    }
}
